using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
using TuyennvLibrary.ServerApi.Sms;
using TuyennvLibrary.SqlClient;
namespace TuyennvLibrary.ServerApi.Models
{
    public class MTLog
    {
        public long mtId { get; set; }
        public long moId { get; set; }
        public string content { get; set; }
        public int status { get; set; }
        public int type { get; set; }
        public DateTime createddate { get; set; }


        public static string COLUMN_MTID = "mtId";
        public static string COLUMN_MOID = "moId";
        public static string COLUMN_CONTENT = "content";
        public static string COLUMN_STATUS = "status";
        public static string COLUMN_TYPE = "type";
        public static string COLUMN_CREATEDDATE = "createddate";


        public static string TableName = "MTLog";

        public MTLog()
        {
			this.mtId = 0;
			this.moId = 0;
			this.content = string.Empty;
			this.status = 0;
			this.type = 0;
			this.createddate = DateTime.Now;
                        
        }
        public MTLog(long moId,MtInfo mt)
        {
            this.mtId = 0;
            this.moId = moId;
            this.content = mt.content;
            this.status = mt.msg_type;
            this.type = mt.content_type;
            this.createddate = DateTime.Now;

        }
    }
    public class mt_logModel:BaseModel<MTLog>
    {
        public mt_logModel()
        {
            this.table = MTLog.TableName;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Insert(MTLog obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter("moId",obj.moId),
					new SqlParameter("content",obj.content),
					new SqlParameter("status",obj.status),
					new SqlParameter("type",obj.type),
					new SqlParameter("createddate",obj.createddate),
                    
                });
                return true;
                
            
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Insert(long moId, MsgContent msgcontent)
        {
            try
            {
                foreach (MtInfo mt in msgcontent.getListMT())
                {
                    MTLog obj = new MTLog(moId, mt);
                    Insert(obj);

                }
                return true;
            }
            catch (Exception)
            {
            }
            return false;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Update(MTLog obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter("moId",obj.moId),
					new SqlParameter("content",obj.content),
					new SqlParameter("status",obj.status),
					new SqlParameter("type",obj.type),
					new SqlParameter("createddate",obj.createddate),

                },new SqlParameter[]{
					new SqlParameter("mtId",obj.mtId),
																									 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Delete(long mtId)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter("mtId",mtId),
																									 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
		public bool DeleteMulti(string ListID)
        {
            try
            {
				if(ListID!=string.Empty)
				{
					foreach(string ID in ListID.Split(','))
					{
						Delete(int.Parse(ID));
					}
				}
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<MTLog> GetByPage(int page, int pagesize, List<MTLog> source)
        {
            List<MTLog> list = new List<MTLog>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
		public List<MTLog> GetListBy(DataTable dt)
		{
			List<MTLog> list = new List<MTLog>();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        MTLog obj = new MTLog();
						obj.mtId= (dr["mtId"] is DBNull) ?0 : long.Parse(dr["mtId"].ToString());
						obj.moId= (dr["moId"] is DBNull) ?0 : long.Parse(dr["moId"].ToString());
						obj.content= (dr["content"] is DBNull) ?string.Empty : dr["content"].ToString();
						obj.status= (dr["status"] is DBNull) ?0 : int.Parse(dr["status"].ToString());
						obj.type= (dr["type"] is DBNull) ?0 : int.Parse(dr["type"].ToString());
						obj.createddate= (dr["createddate"] is DBNull) ?DateTime.Now : DateTime.Parse(dr["createddate"].ToString());
         
                        list.Add(obj);
                    }
                }
                return list;
		}
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);                
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
		public MTLog GetBy(DataTable dt)
		{
			if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        MTLog obj = new MTLog();
						obj.mtId= (dr["mtId"] is DBNull) ?0 : long.Parse(dr["mtId"].ToString());
						obj.moId= (dr["moId"] is DBNull) ?0 : long.Parse(dr["moId"].ToString());
						obj.content= (dr["content"] is DBNull) ?string.Empty : dr["content"].ToString();
						obj.status= (dr["status"] is DBNull) ?0 : int.Parse(dr["status"].ToString());
						obj.type= (dr["type"] is DBNull) ?0 : int.Parse(dr["type"].ToString());
						obj.createddate= (dr["createddate"] is DBNull) ?DateTime.Now : DateTime.Parse(dr["createddate"].ToString());

                        return obj;
                    }
                }
			return new MTLog();
		}
        public MTLog GetByID(long mtId)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter("mtId",mtId),
																									
                },null);
                return GetBy(dt);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}
